using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace MimeCloud.AssetManagement.Business.Helpers
{
    public class Serialization
    {
        public static XElement GenerateSimpleAssetXml(string assetID, string versionName, string assetType)
        {
            XElement root =
                new XElement("Asset",
                    new XAttribute("ID", assetID),
                    new XAttribute("VersionName", versionName),
                    new XAttribute("AssetType", assetType),
                    new XAttribute("SerializationVersion", 1));

            return root;
        }

        public static XElement GenerateSimpleAssetXml(string assetID, string versionName, string assetType, string imageText)
        {
            XElement root =
                new XElement("Asset",
                    new XAttribute("ID", assetID),
                    new XAttribute("VersionName", versionName),
                    new XAttribute("AssetType", assetType),
                    new XAttribute("ImageText", imageText),
                    new XAttribute("SerializationVersion", "1.1"));

            return root;
        }

        public static XElement GenerateSimpleAssetXml(SimpleAssetReference assetReference)
        {
            return GenerateSimpleAssetXml(assetReference.ID, assetReference.VersionName, assetReference.Type);
        }

        public static SimpleAssetReference ParseSimpleAssetXml(string xml)
        {
            SimpleAssetReference simpleAssetReference = null;

            if (!string.IsNullOrEmpty(xml))
            {
                XElement assetRoot = XElement.Parse(xml);
                simpleAssetReference = GetSimpleAssetReference(assetRoot);            
            }
            
            return simpleAssetReference ?? new SimpleAssetReference();
        }

        private static SimpleAssetReference GetSimpleAssetReference(XElement assetRoot)
        {
            SimpleAssetReference simpleAssetReference = new SimpleAssetReference();
            var serializationVersion = assetRoot.Attribute("SerializationVersion") != null
                                               ? assetRoot.Attribute("SerializationVersion").Value
                                               : "1";

            switch (serializationVersion)
            {
                case "1.1":
                case "1":

                    simpleAssetReference.VersionName = assetRoot.Attribute("VersionName") != null
                                                           ? assetRoot.Attribute("VersionName").Value
                                                           : string.Empty;

                    simpleAssetReference.ID = assetRoot.Attribute("ID") != null
                                                  ? assetRoot.Attribute("ID").Value
                                                  : "";

                    simpleAssetReference.Type = assetRoot.Attribute("AssetType") != null
                                                    ? assetRoot.Attribute("AssetType").Value
                                                    : "";

                    if (string.IsNullOrEmpty(simpleAssetReference.Type))
                        simpleAssetReference.Type = "Image";

                    break;
            }

            if (serializationVersion.Equals("1.1"))
            {
                simpleAssetReference.ImageText = assetRoot.Attribute("ImageText") != null
                                 ? assetRoot.Attribute("ImageText").Value
                                 : string.Empty;
            }
            
            return simpleAssetReference;
        
        }

        public static IList<SimpleAssetReference> ParseSimpleAssetsXml(string xml, int takeCount)
        {
            XElement assetsElement = XElement.Parse(xml);
            return assetsElement.Elements("Asset")
                .Take(takeCount)
                .Select(a => GetSimpleAssetReference(a))
                .ToList();
        }
    }
}
